/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: method_return.h
 *
 * Purpose: return template implementation for method
 *
 * Developer:
 *   wen.gu , 2021-05-10
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __METHOD_RETURN_H__
#define __METHOD_RETURN_H__

#include <future>
#include "type_defs.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/


/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/



/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/
template<typename ReturnType>
class Return final
{
public:
    using ReturnHanlder = std::function<void(const ReturnType&)>;
    using SetReplyHandler = std::function<bool(SessionId, ReturnHanlder handler)>;
private:
    std::future<ReturnType> real_ret_;
    SessionId session_id_;
    SetReplyHandler set_reply_handler_;
public:
    Return(std::future<ReturnType>&& output, SessionId id, SetReplyHandler handler)
    : real_ret_(std::move(output)),
    session_id_(id),
    set_reply_handler_(handler)
    {
        /** todo something */
    }

    Return(Return&& other)
    :real_ret_(std::move(other.real_ret_)),
    session_id_(other.session_id_),
    set_reply_handler_(std::move(other.set_reply_handler))
    {
        /** todo something */
    }


    ~Return()
    {
        /** todo something */
    }
public:
    Return& operator= (Return&& other)
    {
        real_ret_ = std::move(other.real_ret_);
        session_id_ = other.session_id_;
        set_reply_handler_ = std::move(other.set_reply_handler_);
    }
public:
    bool setHandler(ReturnHanlder handler)
    {
        if (set_reply_handler_)
        {
            return set_reply_handler_(session_id_, handler);
        }

        return false;
    }

public:
    bool valid() const noexcept
    {
        return real_ret_.valid();
    }

    bool is_ready() const
    {
        return (real_ret_.wait_for(std::chrono::milliseconds::zero()) == std::future_status::ready);
    }

    void wait() const
    {
        real_ret_.wait();
    }

    template <typename Rep, typename Period>
    std::future_status wait_for(std::chrono::duration< Rep, Period > const& timeoutDuration) const
    {
        return real_ret_.wait_for(timeoutDuration);
    }

    template <typename Clock, typename Duration>
    std::future_status wait_until(std::chrono::time_point< Clock, Duration > const& deadline) const
    {
        return real_ret_.wait_until(deadline);
    }

    ReturnType get()
    {
        return real_ret_.get();
    }
};

#endif /** !__METHOD_RETURN_H__ */

